package info

import (
	"openzbox.com/utils/ps/cpu"
	"openzbox.com/utils/ps/disk"
	phost "openzbox.com/utils/ps/host"
	"openzbox.com/utils/ps/mem"
	"openzbox.com/utils/ps/net"

	//"openzbox.com/utils/ps/net"
	lg "openzbox.com/log"
	"time"
)

var (
	HOSTNAME string = ""
)

type SysInfo struct {
	Mem   *mem.VirtualMemoryStat `json:"mem"`
	Cpus  []cpu.InfoStat         `json:"cpus"`
	Disks []disk.UsageStat       `json:"disks"`
	//Disk2 *disk.UsageStat
	Host          *phost.InfoStat                `json:"host"`
	NetInterfaces []net.InterfaceStat            `json:"netinterfaces"`
	NetIO         []net.IOCountersStat           `json:"netio"`
	Protocols     []net.ProtoCountersStat        `json:"protocols"`
	DiskIO        map[string]disk.IOCountersStat `json:"diskio"`
	Partitions    []disk.PartitionStat           `json:"partitions"`
}

func GetSysinfo() (info *SysInfo) {
	v, _ := mem.VirtualMemory()
	cpu, _ := cpu.Info()
	disk1, disk1err := disk.Usage("/mnt/mmcblk0p1")
	disk2, _ := disk.Usage("/mnt/mtdblock2")
	hinfo, _ := phost.Info()
	net1, _ := net.Interfaces()
	netio, _ := net.IOCounters(true)
	protoc, _ := net.ProtoCounters(nil)
	diskio, _ := disk.IOCounters()
	partitions, _ := disk.Partitions(true)
	//cc, _ := cpu.Percent(time.Second, false)
	//nv, _ := net.IOCounters(true)
	s := &SysInfo{}
	s.Mem = v
	s.Cpus = cpu
	s.Host = hinfo
	if disk1err != nil {
		s.Disks = []disk.UsageStat{*disk2}
	} else {
		s.Disks = []disk.UsageStat{*disk1, *disk2}
	}
	s.NetInterfaces = net1
	s.Protocols = protoc
	s.NetIO = netio
	s.DiskIO = diskio
	s.Partitions = partitions
	//lg.Info(s);
	return s
}

func PrintSysinfo() {

	v, _ := mem.VirtualMemory()
	c, _ := cpu.Info()
	disk1, disk1ex := disk.Usage("/mnt/mmcblk0p1")
	disk2, disk2ex := disk.Usage("/mnt/mtdblock2")

	n, _ := phost.Info()
	cc, _ := cpu.Percent(time.Second, false)
	nv, _ := net.IOCounters(true)
	boottime, _ := phost.BootTime()

	HOSTNAME = n.Hostname
	btime := time.Unix(int64(boottime), 0).Format("2006-01-02 15:04:05")

	lg.Infof("************OPENZBOX %s SYSTEM INFOMATION", HOSTNAME)

	lg.Infof("Mem: %v MB  Free: %v MB Usage:%.2f%%\n", v.Total/1024/1024, v.Free/1024/1024, v.UsedPercent)
	if len(c) > 1 {
		for _, sub_cpu := range c {
			modelname := sub_cpu.ModelName
			cores := sub_cpu.Cores
			lg.Infof("CPU: %v   %v cores \n", modelname, cores)
		}
	} else {
		sub_cpu := c[0]
		modelname := sub_cpu.ModelName
		cores := sub_cpu.Cores
		lg.Infof("CPU: %v   %v cores \n", modelname, cores)

	}
	lg.Infof("CPU Used    : used %.2f%% \n", cc[0])

	if disk1ex != nil {
		lg.Warn("not found TF Card.")
	} else {
		lg.Infof("HD(mmcblk0p1): %v MB  Free: %v MB Usage:%.2f%%\n", disk1.Total/1024/1024, disk1.Free/1024/1024, disk2.UsedPercent)
	}
	if disk2ex != nil {
		//lg.Warn("not found TF Card.")
	} else {
		lg.Infof("HD(mtdblock2): %v MB  Free: %v MB Usage:%.2f%%\n", disk2.Total/1024/1024, disk2.Free/1024/1024, disk2.UsedPercent)
	}

	lg.Infof("OS: %v \n", n)

	lg.Infof("Network: %v bytes / %v bytes\n", nv[0].BytesRecv, nv[0].BytesSent)
	lg.Infof("SystemBoot:%v\n", btime)

	lg.Infof("Hostname  : %v  \n", n.Hostname)
	// fmt.Println(v)

}
